#ifndef __H_GAME_H__
#define __H_GAME_H__

#include <deque>
#include <vector>
#include <map>
#include "ICC_Timer_Handler.h"
#include "RedisClient.h"
#include "GameBase.h"
#include "DbPart.h"
#include "CClient.h"
#include "def.h"
#include "LogicData.h"


class CHost;
class CGameServer : BYTimerOject
{
public:
	CGameServer();
	virtual ~CGameServer();
public:
	// callback && interface
	virtual bool InitGame();
	virtual int OnBYTimeOut(int Timerid);
	int ProcessPacket(SocketHandler * pHandler, NETInputPacket* inputPkg);

public:
	int ClientRegister(ServerData* pItem);
	int ClientHandlerClose(SocketHandler* pHandler);
	int RegisterProvider(ServerData* pData, ProviderItem* pProvider);
	int RegisterRequestor(ServerData* pData, RequestorItem* pRequestor);
	int ProviderHandlerClose(ProviderItem* pProvider, int server_id, int server_type);
	int RequestorHandlerClose(RequestorItem* pRequestor, int server_id, int server_type);
	int ProcRetireService(SocketHandler * pHandler, NETInputPacket* inputPkg);
	int ProcLoadConf(SocketHandler * pHandler, NETInputPacket* inputPkg);

	int ProcAddScore(SocketHandler * pHandler, NETInputPacket* inputPkg);
	int ProcReportScore(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcSMOffline(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcVoteLeader(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcSynProviderData(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcReqProviderData(SocketHandler* pHandler, NETInputPacket* inputPkg);
public:
	int PublishService(int provider_type);
	int NotifyRequestorNewService(int provider_type);
	int FindRequestorAllService(int requestor_type, int svid);
	
	int CreateAllProviderDataToPkg(NETOutputPacket* pPkg);
	int SendAllProviderDataToFollower();
	int ReqLeaderProviderData();
	int VoteLeader();
	int ReportScoreToPeers();
public:
	bool ReadCommonConf(const char* strxml);
	int ParseConnStr(std::string& items_str, std::vector<int>& vec_items);
	ProviderItem* GetProviderConfItem(int type);
	RequestorItem* GetRequestorConfItem(int type);
	int GetActiveProviderCount(ProviderVect& activeItems);
	void AddActiveProvider(ProviderVect& activeItems, ServerData* pServer);
	void ResortProviderVect(ProviderVect& activeItems);

public:
	ProviderConfList m_providerConfList;
	RequestorConfList m_requestorConfList;

	
	ProviderMapTypeList m_idleProviderMapTypeList;
	ProviderTypeList m_activeProviderTypeList;
	RequestorTypeList m_requestorTypeList;

	SMList m_smList;
public:
	bool m_isLeader;
	int  m_score;
	CHost* m_pHost;
};


#endif


